package at.tugraz.akm.forume;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import at.tugraz.akm.forume.adapter.ReplyAdapter;
import at.tugraz.akm.forume.bean.Reply;
import at.tugraz.akm.forume.bean.Thread;
import at.tugraz.akm.forume.view.PullToRefreshListView;
import at.tugraz.akm.forume.view.PullToRefreshListView.OnRefreshListener;
import at.tugraz.akm.forume.webservice.Client;

public class ThreadView extends ListActivity {

	ArrayList<Reply> replies;
	int thread_id;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thread);
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		thread_id = getIntent().getExtras().getInt("id");
		// thread_id = 4;

		final Thread thread = Client.getThread(getApplicationContext(),
				thread_id);

		boolean subscribed = Client.subscribed(this, thread_id);

		final Button subscribe = (Button) findViewById(R.id.subscribe);
		final TextView subscribed_ok = (TextView) findViewById(R.id.subscribed_ok);
		final TextView subscribed_error = (TextView) findViewById(R.id.subscribed_error);

		if (subscribed) {
			subscribe.setVisibility(View.GONE);
		} else {
			subscribe.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					subscribe.setVisibility(View.GONE);
					if (Client.subs(getApplicationContext(), thread.getUrl())) {
						subscribed_ok.setVisibility(View.VISIBLE);
					}else{
						subscribed_error.setVisibility(View.VISIBLE);
					}
				}
			});
		}

		TextView title = (TextView) findViewById(R.id.title);
		TextView content = (TextView) findViewById(R.id.content);

		final ImageView new_comment = (ImageView) findViewById(R.id.comment);

		final TextView notify = (TextView) findViewById(R.id.txt_notify);

		final EditText comment = (EditText) findViewById(R.id.new_comment);

		final LinearLayout ll_comment = (LinearLayout) findViewById(R.id.ll_new_comment);
		new_comment.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				notify.setText("");
				if (ll_comment.getVisibility() == View.VISIBLE) {
					ll_comment.setVisibility(View.GONE);
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(comment.getWindowToken(), 0);
				} else {
					ll_comment.setVisibility(View.VISIBLE);
					comment.requestFocus();
					InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					mgr.showSoftInput(comment, InputMethodManager.SHOW_IMPLICIT);
				}
			}
		});

		title.setText(thread.getTitle());
		content.setText(thread.getContent());

		((PullToRefreshListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {
					@Override
					public void onRefresh() {
						new GetNewRepliesTaks().execute();
					}
				});

		replies = thread.getReplyList();

		ReplyAdapter ta = new ReplyAdapter(this, R.layout.reply_row, replies);
		setListAdapter(ta);

		Button bt_send = (Button) findViewById(R.id.bt_send);
		bt_send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (comment.getText().toString().length() > 0) {
					String content = comment.getText().toString();
					if (Client.comment(getApplicationContext(), thread.getId(),
							content)) {
						Log.e("bt_send", "Enviado");
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(comment.getWindowToken(), 0);
						comment.setText("");
						ll_comment.setVisibility(View.GONE);
						replies = Client.getThread(getApplicationContext(),
								thread_id).getReplyList();
						ReplyAdapter ta = new ReplyAdapter(
								getApplicationContext(), R.layout.thread_row,
								replies);
						setListAdapter(ta);
						((PullToRefreshListView) getListView())
								.onRefreshComplete();
						notify.setText(getResources().getString(
								R.string.sent_ok));
					} else {
						Log.e("bt_send", "No enviado");
					}
				} else {
					findViewById(R.id.error_reply_empty).setVisibility(
							View.VISIBLE);
				}
			}
		});

		ImageView ic_url = (ImageView) findViewById(R.id.get_url);
		ic_url.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				final Dialog dialog = new Dialog(ThreadView.this);
				dialog.setContentView(R.layout.dialog_url);
				dialog.setTitle(R.string.get_code);

				TextView tv_code = (TextView) dialog.findViewById(R.id.code);
				tv_code.setText(thread.getUrl());
				tv_code.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
						clipboard.setText(thread.getUrl());
						dialog.findViewById(R.id.copied).setVisibility(
								View.VISIBLE);
					}
				});

				ImageView ic_share = (ImageView) dialog
						.findViewById(R.id.ic_share);
				ic_share.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent sharingIntent = new Intent(Intent.ACTION_SEND);
						sharingIntent.setType("text/plain");
						sharingIntent.putExtra(
								android.content.Intent.EXTRA_SUBJECT,
								v.getContext().getString(R.string.shared_via));
						sharingIntent.putExtra(
								android.content.Intent.EXTRA_TEXT,
								getString(R.string.share_code_a)
										+ thread.getTitle()
										+ getString(R.string.share_code_b)
										+ thread.getUrl());
						v.getContext().startActivity(
								Intent.createChooser(
										sharingIntent,
										v.getContext().getString(
												R.string.share_it)));
					}
				});

				dialog.show();

			}
		});
	}

	public class GetNewRepliesTaks extends AsyncTask<Void, Void, Void> {
		@Override
		protected Void doInBackground(Void... params) {
			try {
				java.lang.Thread.sleep(2000);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {
			Toast.makeText(getApplicationContext(), "Updated",
					Toast.LENGTH_SHORT).show();

			replies = Client.getThread(getApplicationContext(), thread_id)
					.getReplyList();
			ReplyAdapter ta = new ReplyAdapter(getApplicationContext(),
					R.layout.thread_row, replies);
			setListAdapter(ta);
			((PullToRefreshListView) getListView()).onRefreshComplete();
			super.onPostExecute(voids);
		}
	}

}
